/**
 * Created by AllanXu on 01/18/2016.
 */
define(function (require) {
    var Vue = require('vue'),
        Config = require('config'),
        _ = require('ajax'),
        utilities = require('utilities');

    var addCode = sessionStorage.getItem("addCode")?sessionStorage.getItem("addCode"):"33";

    // 认证
    var AuthDialog = require('auth_dialog');
    var authDialog = new AuthDialog();
    // 禁用申诉
    var Forbidden = require('forbidden');
    var forbidden = new Forbidden();

    var app = new Vue({
        el: '#mood',
        data: {
            previewImg: '',
            pageCount: '',
            pageno: 1,
            renderObj: {
                listArr: []
            },
            bottomTxt: '',
            showComment: false,
            showCommentIndex: '',
            leaveWords: '',
            // showPublic: false,
            publicContent: '',
            picServer: Config.picServer,
            defaultAdd: '江西省',
            msgid:'',
            userInfo: ''
        },
        methods: {
            init: function () {
                // app.userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                //             app.$log();

                // 右上角地区
                var addName = sessionStorage.getItem('addName');
                app.defaultAdd = addName || app.defaultAdd;

                _(Config.SERVICE.INDEX.MYMOOD, {
                    "pagesize": 12,
                    "pageno": app.pageno,
                    "code": addCode
                }, function (data) {
                    // console.log(data);
                    if (app.pageno == 1) {
                        app.renderObj.listArr = data.pageitems;
                        app.pageCount = data.pagecount;
                    } else {
                        app.renderObj.listArr = app.renderObj.listArr.concat(data.pageitems);
                    }
                });
            },
            goDetail: function (id) {
                location.href = "activity_photo_detail.html?id=" + id;
            },
            preview: function (imgurl) {
                // var IMGURL = 'http://appapp.b0.upaiyun.com'+imgurl;
                // console.log(IMGURL);
                // // var IMGARR = app.mainInfo.zp.ann
                // _(Config.SERVICE.COMMON.WX_SIGN, {
                //     // 'openId': this.openId,
                //     'url': encodeURIComponent(location.href)
                // }, function (data) {
                //     console.log(data);
                //     wx.ready(function () {
                //         wx.previewImage({
                //             current: IMGURL, // 当前显示图片的http链接
                //             urls: [IMGURL] // 需要预览的图片http链接列表
                //         });
                //     });
                //     wx.config({
                //         debug: false,
                //         appId: data.obj.appid,
                //         timestamp: data.obj.timestamp,
                //         nonceStr: data.obj.nonceStr,
                //         signature: data.obj.signature,
                //         jsApiList: ['previewImage']
                //     });
                // });
                app.previewImg = app.picServer + imgurl;
            },
            closeImg: function () {
                app.previewImg = '';
            },
            toComment: function (index) {
                var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                if (userInfo.member.sfgb == 1) {	// 认证
                    if (confirm("您已脱单，如需继续使用，请点击恢复单身。")) {
                        location.href = 'uc_index.html';
                    }
                    return;
                }
                // 是否认证/禁用
                // base64TojsonObj
                /*var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                if (userInfo.member.sfrz == 0) {	// 认证
                    authDialog.show();
                } else if (userInfo.member.sfjy == 1) {	// 禁用
                    forbidden.show();
                } else {
                    // 已认证、没有禁用
                    _(Config.SERVICE.MATES.VERIFY + '?id=' + sessionStorage.id, {}, function (data) {
                        console.log(data, 2444444);
                        if (data.errmsg.indexOf("择偶") > 0) {
                            // 择偶条件没有完善跳转到择偶条件
                            if (confirm(data.errmsg)) {
                                location.href = 'uc_mate_condition.html';
                            }
                        } else if (data.errmsg.indexOf("基本信息") > 0) {
                            if (confirm(data.errmsg)) {
                                location.href = 'uc_profile.html';
                            }
                        } else {*/
                            app.msgid='';
                            if (app.showCommentIndex === index) {
                                return app.showCommentIndex = '';
                            } else {
                                app.showCommentIndex = index;
                                app.msgid='';
                                app.showComment = !app.showComment;
                            }
                        /*}
                    });
                }*/
            },
            toCommentHf: function (index,num) {
                var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                if (userInfo.member.sfgb == 1) {	// 认证
                    if (confirm("您已脱单，如需继续使用，请点击恢复单身。")) {
                        location.href = 'uc_index.html';
                    }
                    return;
                }
                // 是否认证/禁用
                // base64TojsonObj
               /* var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                if (userInfo.member.sfrz == 0) {	// 认证
                    authDialog.show();
                } else if (userInfo.member.sfjy == 1) {	// 禁用
                    forbidden.show();
                } else {
                    // 已认证、没有禁用
                    _(Config.SERVICE.MATES.VERIFY + '?id=' + sessionStorage.id, {}, function (data) {
                        if (data.errmsg.indexOf("择偶") > 0) {
                            // 择偶条件没有完善跳转到择偶条件
                            if (confirm(data.errmsg)) {
                                location.href = 'uc_mate_condition.html';
                            }
                        } else if (data.errmsg.indexOf("基本信息") > 0) {
                            if (confirm(data.errmsg)) {
                                location.href = 'uc_profile.html';
                            }
                        } else {*/
                            if (app.showCommentIndex === index) {
                                return app.showCommentIndex = '';
                            } else {
                                app.showCommentIndex = index;
                                app.msgid=num;
                                app.showComment = !app.showComment;
                            }
                       /* }
                    });
                }*/
            },
            comment: function (dyId) {
                _(Config.SERVICE.INDEX.MOODMSGSAVE, {
                    "id": sessionStorage.id,
                    'dynamicMsg.content': app.leaveWords,
                    'dynamicMsg.msg.id': app.msgid,
                    "dynamicMsg.dyId": dyId
                }, function (data) {
                    if (data.code == 200) {
                        alert("发送成功~")
                        app.init();
                        app.leaveWords = '';
                    }
                });
            },
            switchPublic: function () {
                // 是否认证/禁用
                var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                if (userInfo.member.sfgb == 1) {	// 认证
                    if (confirm("您已脱单，如需继续使用，请点击恢复单身。")) {
                        location.href = 'uc_index.html';
                    }
                    return;
                }
                // base64TojsonObj
                /*var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                if (userInfo.member.sfrz == 0) {	// 认证
                    authDialog.show();
                } else if (userInfo.member.sfjy == 1) {	// 禁用
                    forbidden.show();
                } else {
                    console.log("已认证、没有禁用");
                    _(Config.SERVICE.MATES.VERIFY + '?id=' + sessionStorage.id, {}, function (data) {
                        console.log(data, 2444444);
                        if (data.errmsg.indexOf("择偶") > 0) {
                            // 择偶条件没有完善跳转到择偶条件
                            if (confirm("为更好地为您提供服务，请在“个人编辑”中完善择偶条件。")) {
                                location.href = 'uc_mate_condition.html';
                            }
                        } else if (data.errmsg.indexOf("基本信息") > 0) {
                            if (confirm("为更好地为您提供服务，请在“个人编辑”中完善个人资料。")) {
                                location.href = 'uc_profile.html';
                            }
                        }else if(data.errmsg.indexOf("形象照") > 0){
                            if(confirm("为更好地为您提供服务，请在“个人编辑”中上传形象照。")){
                                location.href = 'uc_basicInfo.html';
                            }
                        } else {*/
                            // app.showPublic = !app.showPublic;
                            // 弹窗变跳转
                            location.href = 'publish.html'
                        /*}
                    });
                }*/
            },
            sendPublic: function () {
                _(Config.SERVICE.INDEX.MOODPUBLIC, {
                    "id": sessionStorage.id,
                    'dynamic.content': app.publicContent,
                }, function (data) {
                    console.log(data);
                    if (data.code == 200) {
                        alert("发布成功~")
                        app.init();
                        app.publicContent = '';
                        // app.showPublic = false;
                    }
                });
            },
            goUserDetail: function (uid) {
                location.href = 'friend_detail.html?id=' + uid;
            }
        }
    });
    app.init();

    window.onscroll = function () {
        var scrolltop = document.body.scrollTop | document.documentElement.scrollTop;
        var clientHeight = document.documentElement.clientHeight;
        if (scrolltop + clientHeight == document.body.clientHeight) {
            if (app.pageno <= app.pageCount) {
                app.pageno++;
                app.init();
            } else {
                app.bottomTxt = '到底儿了~';
            }
        }
    }
});